<template>
	<div class="wrapper">
		<div class="back-icon" @click="goBack()">
			<i class="fa fa-arrow-left"></i>
		</div>
		<!-- header部分 -->
		<header>
			<p>地址管理</p>
		</header>

		<!-- 地址列表部分 -->
		<ul class="addresslist">
			<li v-for="item in deliveryAddressArr">
				<div class="addresslist-left" @click="setDeliveryAddress(item)">
					<h3>{{item.contactName}}{{item.contactSex==1?"先生":"女士"}} {{item.contactTel}}</h3>
					<p>{{item.address}}</p>
				</div>
				<div class="addresslist-right">
					<i class="fa fa-edit" @click="editUserAddress(item.daId)"></i>
					<i class="fa fa-remove" @click="removeUserAddress(item.daId)"></i>
				</div>
			</li>
		</ul>

		<!-- 新增地址部分 -->
		<div class="addbtn" @click="toAddUserAddress">
			<i class="fa fa-plus-circle"></i>
			<p>新增店铺地址</p>
		</div>

		<!-- 底部菜单部分 -->
		<MsFooter></MsFooter>
	</div>
</template>

<script>
	import MsFooter from '@/components/MsFooter.vue'
	import {useRouter} from 'vue-router'
	import {useRoute} from 'vue-router'
	import {ref,onMounted,getCurrentInstance, reactive} from 'vue';
	
	export default{
		setup() {
			const currentInstance = getCurrentInstance();
			const router = useRouter();
			const route = useRoute();
			const businessId = route.params.businessId;
			const user = ref({});
			const deliveryAddressArr = ref([]);
			
			const goBack = () => {
				router.go(-1);
			}
			
			const listDeliveryAddressByUserId = () => {
				//查询送货地址
				currentInstance.appContext.config.globalProperties.$axios.post(
							'DeliveryAddressController/listDeliveryAddressByUserId', currentInstance.appContext.config
							.globalProperties.$qs.stringify({
								userId: user.value.userId
							}))
						.then(response => {
							deliveryAddressArr.value = response.data;
						})
						.catch(error => {
							console.error(error);
						});
			}
			
			const setDeliveryAddress = (deliveryAddress) => {
				//把用户选择的默认送货地址存储到localStorage中
				currentInstance.appContext.config.globalProperties.$setLocalStorage(user.value.userId,deliveryAddress);
				if(businessId==0){
					router.push({name:'MerchantCenterVue'});
				}else{
					router.push({name:'Orders',params:{businessId:businessId}});
				}
			}
			
			const toAddUserAddress = () => {
				router.push({name:'AddMcAddressVue',params:{businessId:businessId}});
			}
			
			const editUserAddress = (daId) => {
				router.push({name:'EditMcAddressVue',params:{businessId:businessId,daId:daId}});
			}
			
			const removeUserAddress = (daId) => {
				if(!confirm('确认要删除此送货地址吗？')){
					return;
				}
				
				currentInstance.appContext.config.globalProperties.$axios.post(
							'DeliveryAddressController/removeDeliveryAddress', currentInstance.appContext.config
							.globalProperties.$qs.stringify({
								daId: daId
							}))
						.then(response => {
							if(response.data>0){
								let deliveryAddress = currentInstance.appContext.config.globalProperties.$getLocalStorage(user.value.userId);
								if(deliveryAddress!=null&&deliveryAddress.daId==daId){
									currentInstance.appContext.config.globalProperties.$removeLocalStorage(user.value.userId);
								}
								listDeliveryAddressByUserId();
							}else{
								alert('删除地址失败！');
							}
							deliveryAddressArr.value = response.data;
						})
						.catch(error => {
							console.error(error);
						});
			}
			
			onMounted(() => {
				user.value = currentInstance.appContext.config.globalProperties.$getSessionStorage('user');
				// 根据orderTypeId查询商家信息
				listDeliveryAddressByUserId();
			});
			
			return{
				businessId,user,deliveryAddressArr,
				listDeliveryAddressByUserId,setDeliveryAddress,toAddUserAddress,editUserAddress,removeUserAddress,goBack
			}
		},
		components: {
			MsFooter
		},
	}
</script>

<style scoped>
	/*************** 总容器 ***************/
	.wrapper {
		width: 100%;
		height: 100%;
		background-color: #F5F5F5;
	}
	/*********** 回退按钮 ***********/
	.wrapper .back-icon{
		width: 12vw;
		height: 12vw;
		background-color: #0097FF;
		color: #FFFFFF;
		font-size: 4.8vw;
		position: fixed;
		left: 0;
		top: 0;
		z-index: 1001;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	/*************** header ***************/
	.wrapper header {
		width: 100%;
		height: 12vw;
		background-color: #0097FF;
		display: flex;
		justify-content: space-around;
		align-items: center;
		color: #fff;
		font-size: 4.8vw;
		position: fixed;
		left: 0;
		top: 0;
		/*保证在最上层*/
		z-index: 1000;
	}

	/*************** addresslist ***************/
	.wrapper .addresslist {
		width: 100%;
		margin-top: 12vw;
		background-color: #fff;
	}

	.wrapper .addresslist li {
		width: 100%;
		box-sizing: border-box;
		border-bottom: solid 1px #DDD;
		padding: 3vw;
		display: flex;
	}

	.wrapper .addresslist li .addresslist-left {
		flex: 5;
		/*左边这块区域是可以点击的*/
		user-select: none;
		cursor: pointer;
	}

	.wrapper .addresslist li .addresslist-left h3 {
		font-size: 4.6vw;
		font-weight: 300;
		color: #666;
	}

	.wrapper .addresslist li .addresslist-left p {
		font-size: 4vw;
		color: #666;
	}

	.wrapper .addresslist li .addresslist-right {
		flex: 1;
		font-size: 5.6vw;
		color: #999;
		cursor: pointer;
		display: flex;
		justify-content: space-around;
		align-items: center;
	}

	/*************** 新增地址部分 ***************/
	.wrapper .addbtn {
		width: 100%;
		height: 14vw;
		border-top: solid 1px #DDD;
		border-bottom: solid 1px #DDD;
		background-color: #fff;
		margin-top: 4vw;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 4.5vw;
		color: #0097FF;
		user-select: none;
		cursor: pointer;
	}

	.wrapper .addbtn p {
		margin-left: 2vw;
	}
</style>